package cn.com.jrj.app;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

import cn.com.jrj.base.VnetAppContext;
import cn.com.jrj.base.work.VnetAppRunnable;
import cn.com.jrj.base.work.VnetAppWork;


/**
 * 评论频道
 * 
 * @author 
 * 
 */
public class VnetPublishJob extends QuartzJobBean{
	
	Log log = LogFactory.getLog(getClass());

	@SuppressWarnings("unchecked")
	protected void executeInternal(JobExecutionContext context) {
		Map dataMap = context.getJobDetail().getJobDataMap();
		List<String> works = (List<String>) dataMap.get("works");

		ApplicationContext ctx = VnetAppContext.getSpringCtx();		
		ThreadPoolTaskExecutor taskExcutor = VnetAppContext.getTaskExecutor();

		for (String workName : works) {
			VnetAppWork work = (VnetAppWork) ctx.getBean(workName);
			try {
				if (work.attempt()) {// 初步判断任务是否可以执行
					taskExcutor.execute(new VnetAppRunnable(work, null));
				} else {// 不能执行，则丢弃此任务
					work.discard();
				}
			} catch (InterruptedException e) {
				log.error(getClass().getName() + " taskExcutor did not accept task:" + work.getClass().getName());
			}
		}
	}

}
